extends CharacterBody2D

class_name Player

enum PlayerActionEnum{
	INIT,IDEL,RUN,JUMP,DEATH,SLIDE
}

enum PlayerSpeedStatusEnum{
	NORMAL,SPEED
}


const RUN_SPEED := 150.0
const JUMP_VELOCITY := -350.0
# 地板移动速度
const FLOOR_ACCELERATION := 0.6
# 跳跃控制移动速度
const AIR_ACCELERATION := 0.25  # 增加空中控制度
# 跌落
const DROP_ACCELERATION := 0.8
# 停止时的基础减速度
const STOP_MULTIPLIER := 1.8
# 空中移动速度倍率
const AIR_SPEED_MULTIPLIER := 1.2  # 新增空中速度倍率


var gravity := ProjectSettings.get("physics/2d/default_gravity") as float
@onready var camera_sync:Camera2D = $Camera2D

var playerAction = PlayerActionEnum.INIT
var speedStatus = PlayerSpeedStatusEnum.NORMAL

@onready var coyote_timer: Timer = $CoyoteTimer
@onready var jump_request_timer: Timer = $JumpRequestTimer
@onready var animation_player:AnimatedSprite2D = $PlayerAnimated


func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("jump"):
		jump_request_timer.start()




func _physics_process(delta):
	
	# Apply gravity
	if not is_on_floor():
		velocity.y += gravity * delta * DROP_ACCELERATION
	
	# 限制最大下落速度
	velocity.y = minf(velocity.y, 400.0)  # 添加最大下落速度限制
	
	var direction := Input.get_axis("left", "right")
	var acceleration := FLOOR_ACCELERATION if is_on_floor() else AIR_ACCELERATION
	var toDirection = direction * RUN_SPEED
	if not is_on_floor():
		toDirection *= AIR_SPEED_MULTIPLIER  # 空中移动速度提升
	var stopSpeed = RUN_SPEED * STOP_MULTIPLIER
	if speedStatus == PlayerSpeedStatusEnum.SPEED:
		acceleration = acceleration * 1.5
		toDirection = toDirection * 1.5
		stopSpeed = stopSpeed * 1.5  # 降低加速状态下的停止减速倍率
		
	if direction:
		# 没有滑动
		#velocity.x = move_toward(velocity.x, toDirection, acceleration * delta)
		# 移动带延迟滑动
		velocity.x = lerpf(velocity.x, toDirection, acceleration * delta)
	else: 
		# 停下运动
		velocity.x = move_toward(velocity.x, 0, stopSpeed * delta)
	
	

	
	var can_jump := is_on_floor() or coyote_timer.time_left > 0
	var should_jump := can_jump and jump_request_timer.time_left > 0
	
	if should_jump:
		velocity.y = JUMP_VELOCITY
		coyote_timer.stop()
		jump_request_timer.stop()
	
	if is_on_floor() and Input.is_action_just_pressed("jump"):
		velocity.y = JUMP_VELOCITY
	if Input.is_action_just_released("jump") and velocity.y < 0:
		velocity.y *= 0.5
		
	# 加速
	if is_on_floor() and Input.is_action_just_pressed("speed-up"):
		speedStatus = PlayerSpeedStatusEnum.SPEED
	if Input.is_action_just_released("speed-up"):
		speedStatus = PlayerSpeedStatusEnum.NORMAL
		
	
	if is_on_floor():
		if is_zero_approx(direction) and velocity.x == 0:
			playerAction = PlayerActionEnum.IDEL
		elif sign(velocity.x) != sign(direction) && velocity.x != 0 && direction != 0:
			playerAction = PlayerActionEnum.SLIDE
		else:
			playerAction = PlayerActionEnum.RUN
		
	else:
		if playerAction != PlayerActionEnum.INIT:
			playerAction = PlayerActionEnum.JUMP
	
	if not is_zero_approx(direction) and is_on_floor():
		animation_player.flip_h = direction < 0
	
	
	var lastSlideCollision = get_last_slide_collision()
	if lastSlideCollision != null:
		handle_movement_collision(lastSlideCollision)

	
	# 展示动画
	showPlayerAnimation()
	move_and_slide()
	
	var was_on_floor := is_on_floor()
	if is_on_floor() != was_on_floor:
		if was_on_floor and not should_jump:
			coyote_timer.start()	# 发生改变并且是离开地面
		else:
			coyote_timer.stop()		# 发生改变但是落地

func handle_movement_collision(collision: KinematicCollision2D):
	#碰撞最后一个物体
	pass
			

func showPlayerAnimation():
	var animationName = "idle"
	var speed = 1
	match playerAction:
		PlayerActionEnum.IDEL:
			animationName = "idle"
		PlayerActionEnum.RUN:
			animationName = "run"
		PlayerActionEnum.JUMP:
			animationName = "jump"
		PlayerActionEnum.DEATH:
			animationName = "DEATH"
		PlayerActionEnum.SLIDE:
			animationName = "slide"
	if speedStatus == PlayerSpeedStatusEnum.SPEED:
		speed = 3
			
	animation_player.play(animationName,speed)
